<template>
  <div class="list-pages white-bg">
    <div class="search-form-box">
      <a-form
        :model="formState"
        name="horizontal_login"
        :label-col="labelCol"
        autocomplete="off"
        @finish="onFinish"
      >
        <a-row :gutter="[16, 16]">
          <a-col :xs="24" :sm="24" :md="12" :lg="6" :xl="6" v-show="expand">
            <a-form-item label="设备名称" name="keyword">
              <a-input
                v-model:value="formState.keyword"
                placeholder="请输入设备名称"
              >
              </a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="24" :md="12" :lg="6" :xl="6" v-show="expand">
            <a-form-item label="资产编号" name="keyword">
              <a-input
                v-model:value="formState.keyword"
                placeholder="请输入资产编号"
              >
              </a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="24" :md="12" :lg="6" :xl="6" v-show="expand">
            <a-form-item label="设备型号" name="keyword">
              <a-input
                v-model:value="formState.keyword"
                placeholder="请输入设备型号"
              >
              </a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="24" :md="12" :lg="6" :xl="6" v-show="expand">
            <a-form-item label="设备编码" name="keyword">
              <a-input
                v-model:value="formState.keyword"
                placeholder="请输入设备编码"
              >
              </a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="24" :md="12" :lg="6" :xl="6" v-show="expand">
            <a-form-item label="关联实验" name="customer_type_id">
              <a-select
                v-model:value="formState.customer_type_id"
                placeholder="请选择关联实验"
              >
                <a-select-option
                  v-for="(item, index) in classOptions"
                  :key="index"
                  :value="item.value"
                  >{{ item.label }}</a-select-option
                >
              </a-select>
            </a-form-item>
          </a-col>
           <a-col :xs="24" :sm="24" :md="12" :lg="6" :xl="6" v-show="expand">
            <a-form-item label="使用地点" name="customer_type_id">
              <a-select
                v-model:value="formState.customer_type_id"
                placeholder="请选择使用地点"
              >
                <a-select-option
                  v-for="(item, index) in statusOptions"
                  :key="index"
                  :value="item.value"
                  >{{ item.label }}</a-select-option
                >
              </a-select>
            </a-form-item>
          </a-col>
           <a-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12" v-show="expand">
            <a-form-item label="入库时间" name="customer_type_id">
             <a-range-picker/>
            </a-form-item>
          </a-col>
      
          <a-col :xs="24" :sm="24" :md="12" :lg="6" :xl="6">
            <a-form-item>
              <div class="search-btn-box" :class="{ pt: expand }">
                <a-button
                  :icon="h(SearchOutlined)"
                  type="primary"
                  html-type="submit"
                  class="search-btn"
                  >查询</a-button
                >
                <a-button
                  :icon="h(ReloadOutlined)"
                  class="reset-btn"
                  @click="resetForm"
                  >清空</a-button
                >
                <a
                  class="expand-btn"
                  style="font-size: 12px; margin-left: 12px"
                  @click="expand = !expand"
                >
                  <template v-if="expand">
                    <UpOutlined />
                  </template>
                  <template v-else>
                    <DownOutlined />
                  </template>
                  {{ expand ? "收起查询" : "展开查询" }}
                </a>
              </div>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <div class="table-wrapper" ref="tableRef">
      <a-table
        class="table-box"
        row-key="index"
        :columns="columns"
        :dataSource="tableData"
        :row-class-name="
          (_record, index) => (index % 2 === 1 ? 'table-striped' : null)
        "
        sticky
        :scroll="{ y: true, x: 1100 }"
        :pagination="pagination"
        @change="onChange"
      >
        <template #bodyCell="{ column, record }">
          <template v-if="column.dataIndex == 'operation'">
            <a-button type="link" class="link" @click="handleDetail(record)">
              查看
            </a-button>
            <a-button type="link" class="link" @click="handleEdit(record)">
              编辑
            </a-button>
            <a-popconfirm title="确认启用?" @confirm="handleEnable(record)">
              <a-button type="link" class="link"> 启用 </a-button></a-popconfirm
            >
            <a-popconfirm title="确认删除?" @confirm="handleDelte(record)">
              <a-button type="link" class="link"> 删除 </a-button></a-popconfirm
            >
          </template>
        </template>
      </a-table>
    </div>
  </div>
</template>
<script setup>
import { ref, computed, h, onMounted, onUnmounted } from "vue";
import {
  SearchOutlined,
  ReloadOutlined,
  DownOutlined,
  UpOutlined,
} from "@ant-design/icons-vue";
import { useRouter } from "vue-router";
import { useGlobalStore } from "@/store/modules/global";
import API from "@/api/operation";
import SAPI from "@/api/system";
import { getCityName } from "@/utils/city.ts";
const store = useGlobalStore();
const router = useRouter();
const labelCol = { style: { width: "80px" } };

const formState = ref({
  keyword: "",
  dealer_id: null,
  city_id: null,
  customer_status_id: null,
  customer_type_id: null,
});
const resetForm = () => {
  formState.value = {
    keyword: "",
    dealer_id: null,
    city_id: null,
    customer_status_id: null,
    customer_type_id: null,
  };
  getData();
};
//tree结构中获取省市名称



const windowWidth = ref(window.innerWidth);
const expand = ref(true);
onMounted(async () => {
  windowWidth.value = window.innerWidth;
  if (windowWidth.value > 768) {
    expand.value = true;
  } else {
    expand.value = false;
  }
  window.addEventListener("resize", () => {
    windowWidth.value = window.innerWidth;
    if (windowWidth.value > 768) {
      expand.value = true;
    } else {
      expand.value = false;
    }
  });
  // await getTypeOptions();
  // await getZoneData();
  // await getStatusOptions();
  // await getDealerOptions();
  await getData();
});
onUnmounted(() => {
  window.removeEventListener("resize", () => {});
});

function onFinish(values) {
  console.log("Success:", values);
  getData();
}
const tableData = ref([]);

const columns = ref([
  {
    dataIndex: "no",
    title: "设备名称",
    width: 240,
  },
  
  {
    dataIndex: "name",
    title: "设备型号",
    width: 110,
  },
  {
    dataIndex: "customer_type_id",
    title: "资产编号",
    ellipsis: true,
    width: 110,
  },
  {
    dataIndex: "city_id",
    title: "品牌",
    width: 150,
  },
  {
    dataIndex: "customer_status_id",
    title: "设备编码",
    width: 110,
   
  },
  {
    dataIndex: "orders",
    title: "关联实验",
    width: 110,
  },
  {
    dataIndex: "dealer_id",
    title: "入库时间",
    width: 150,
  },
  {
    dataIndex: "date",
    title: "保修状态",
    width: 110,
  },
  {
    dataIndex: "warranty_status",
    title: "保修截止日期",
    width: 110,
  },
  {
    dataIndex: "warranty_end_date",
    title: "使用地点",
    width: 110,
  },
  {
    dataIndex: "use_place",
    title: "设备状态",
    width: 110,
  },
  {
    dataIndex: "operation",
    title: "操作",
    width: 200,
    align: "center",
    fixed: "right",
  },
]);

const onChange = (page) => {
  console.log(page);
  current.value = page.current;
  pageSize.value = page.pageSize;
};
const current = ref(1);
const pageSize = ref(10);
const total = ref(0);
const pagination = computed(() => ({
  position: ["bottomCenter"],
  total: total.value,
  current: current.value,
  pageSize: pageSize.value,
  showSizeChanger: true,
  size: "large",
  showTotal: (total) => `共 ${total} 条`,
}));
// 表格数据获取方法
const getData = async () => {
  
  tableData.value = [
    {
      no:'智能摄像头1',
      name:'v2.1',
      customer_type_id:'232323',
      city_id:'良益科技',
      customer_status_id:'324312',
      orders:'测量波长',
      dealer_id:'2022-02-07',
      date:'在保',
      warranty_status:'2026-02-07',
      warranty_end_date:'第七实验室',
      use_place:'正常',
    },
    {
      no:'智能摄像头2',
      name:'v2.1',
      customer_type_id:'232323',
      city_id:'良益科技',
      customer_status_id:'324312',
      orders:'测量波长',
      dealer_id:'2022-02-07',
      date:'在保',
      warranty_status:'2026-02-07',
      warranty_end_date:'第七实验室',
      use_place:'正常',
    }
  ]
};
const classOptions = ref([{
  label:'测量波长',
  value:'测量波长'
}]);

const statusOptions = ref([{
  label:"第七实验室",
  value:"第七实验室"
}]);



const handleDetail = (data) => {
  store.setSubTitle("查看设备信息");
  router.push({
    path:`ai-experiment-evaluation-device-manage-detail`,
    query:data
  });
};
const handleEdit = (data) => {
  console.log("编辑设备信息", data);
  store.setSubTitle("编辑设备信息");
  router.push({
    path: `ai-experiment-evaluation-device-manage-edit`,
    query:data
  });
};

const handleDelte = (data) => {
  console.log("删除客户", data);
  tableData.value = tableData.value.filter((item) => item.no !== data.no);
};
const handleEnable = (data) => {
  console.log("启用", data);
  data.use_place= '启用中'
};
</script>
<style lang="less" scoped>
.list-pages {
  height: 100%;
  padding: 0 16px;
  display: flex;
  flex-direction: column;
  .search-box {
    display: flex;
    align-items: center;
    padding: 18px 0;
    flex-wrap: wrap;
    border-bottom: 1px solid #e5e7eb;
    .search-select {
      width: 120px;
    }
    .line {
      width: 1px;
      height: 16px;
      margin: 0 12px;
      background: #bec4ca;
      @media screen and (max-width: 768px) {
        display: none;
      }
    }
    .search-input-box {
      flex: 1;
      display: flex;
      align-items: center;
      @media screen and (max-width: 768px) {
        width: 100%;
        margin-top: 16px;
        justify-content: space-between;
      }
    }
    .search-input {
      flex: 1;
    }
    .search-btn {
      width: 60px;
      height: 32px;
      background: #2776ff;
      border-radius: 4px;
      font-family: PingFangSC-Regular;
      font-weight: 400;
      font-size: 14px;
      color: #ffffff;
      text-align: center;
      line-height: 32px;
    }
  }
  .table-wrapper {
    width: 100%;
    flex: 1;
    overflow: hidden;
    .table-box {
      height: 100%;
    }
  }
  .list-box {
    width: 100%;
    box-sizing: border-box;
    padding-top: 16px;
    overflow: auto;
    display: grid;
    gap: 16px;
    margin: 0 auto;
    grid-template-columns: repeat(auto-fill, minmax(261px, 1fr));
    justify-content: space-between;
    .list-item {
      width: 261px;
      background: #ffffff;
      border: 1px solid #e6ebf1;
      border-radius: 8px;
      box-sizing: border-box;
      padding: 8px;
      margin: auto;
      position: relative;
      cursor: pointer;
      &:hover {
        border: 1px solid #2776ff;
        box-shadow: 12px 12px 12px 0px rgba(39, 118, 255, 0.1);
      }
      .item-collect {
        position: absolute;
        top: 0;
        right: 0;
        cursor: pointer;
        width: 32px;
        height: 32px;
        background: rgba(0, 0, 0, 0.4) url(@/assets/icon-collet.png) no-repeat
          center center;
        border-radius: 0 8px 0 12px;
        &.cur {
          background: #ffffff url(@/assets/icon-collet-cur.png) no-repeat center
            center;
          border-radius: 0 8px 0 12px;
        }
      }

      .list-img {
        width: 245px;
        height: 249px;
        background: #f3f3f3;
        border: 1px solid #e6ebf1;
        border-radius: 8px;
        box-sizing: border-box;
        img {
          width: 100%;
          height: 100%;
          border-radius: 8px;
        }
      }
      .list-content {
        padding: 14px 0 6px;
        display: flex;
        flex-direction: column;
        .content-title {
          font-family: PingFangSC-Medium;
          font-weight: 500;
          font-size: 16px;
          color: #31323a;
          margin-bottom: 10px;
          line-height: 24px;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        .content-col {
          display: flex;
          align-items: center;
          justify-content: space-between;
          .col-user {
            display: flex;
            align-items: center;
            .user-img {
              width: 26px;
              height: 26px;
              border-radius: 50%;
            }
            .user-name {
              font-family: PingFangSC, PingFang SC;
              font-weight: 400;
              font-size: 12px;
              color: #262626;
              line-height: 16px;
              text-align: left;
              font-style: normal;
            }
          }
          .col-date {
            font-family: PingFangSC, PingFang SC;
            font-weight: 400;
            font-size: 12px;
            color: #8c8c8c;
            line-height: 16px;
            text-align: center;
            font-style: normal;
          }
        }
      }
    }
  }

  .pagination-box {
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
</style>
